﻿using System;
using System.Web.Http;
using System.Collections.Specialized;
using PreeRegisterModel;
using PreeRegisterBLL;
using PreeRegisterBLL.Security;

namespace PreeRegister_WebAPI.Controllers
{
    public class registerController : ApiController
    {
        [HttpPost]
        /// <summary>
        /// 退费
        /// </summary>
        /// <param name="any"></param>
        /// <returns></returns>
        public EncodeModel fade(NameValueCollection any)
        {
            try
            {
                //获取JSON加密参数
                string jsonData = CommonBLL.RequestGetParam("jsonData").ToString();

                RegisterFadeResponseModel registerFadeResponseModel = new RegisterFadeResponseModel();

                //解密头和内容信息
                string head_json = "";
                string body_json = "";
                head_json = CommonBLL.RequestGetParam("head").ToString();
                body_json = CommonBLL.RequestGetParam("body").ToString();

                jsonData += "{";
                jsonData += string.Format("\"head\":\"{0}\",\"body\":\"{1}\"", head_json, body_json);
                jsonData += "}";
                string ErrMsg = "";
                string ReturnBodyJson = "";
                if (!ResultDecode.DecodeResult(jsonData, "register|fade", ref head_json, ref body_json, ref ErrMsg))
                {
                    //序列化返回内容类
                    ReturnBodyJson = csSerialize.RegisterFadeResponseModel(registerFadeResponseModel);

                    //加密返回内容Json
                    return ResultDecode.EncodeResult(ReturnBodyJson);
                }

                //入口参数日志
                CommonBLL.WriteLog("register|fade|Post|\r\n" + body_json + "\r\n\r\n");

                //反序列化请求字符串
                RegisterFadeModel registerFadeModel = csSerialize.RegisterFadeRequestModel(body_json);

                OrderInfoModel orderInfo = OrderExeInfoBLL.GetParentInfo(CommonBLL.GetOrderId(registerFadeModel.cflowcode, 5));

                //退费
                registerFadeResponseModel = CancelOrderBLL.RegisterFade(orderInfo);

                //序列化返回内容类
                ReturnBodyJson = csSerialize.RegisterFadeResponseModel(registerFadeResponseModel);

                //返回参数日志
                CommonBLL.WriteLog("register|fade|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
            catch (Exception err)
            {
                NotPayResponseModel notPayResponseModel = new NotPayResponseModel();

                //序列化返回内容类
                string ReturnBodyJson = csSerialize.NotPayResponseModel(notPayResponseModel);

                //返回参数日志
                CommonBLL.WriteLog("register|fade|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                //加密返回内容Json
                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
        }
    }
}
